package blueq.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import blueq.dao.IBaseDao;
import blueq.dao.mongodb.impl.MongoDBDaoImpl;
import blueq.entity.User;
import blueq.entity.mongo.AppInfo;
import blueq.entity.mongo.Tree;
import blueq.util.JsonParser;

import com.alibaba.fastjson.JSON;

@Controller
public class TestController {
	@Resource(name = "dao")
	private IBaseDao dao;

	// @Resource(name="natureRepository")
	@Resource(name = "mongoDbDao")
	private MongoDBDaoImpl mongoDao;

	public MongoDBDaoImpl getMongoDao() {
		return mongoDao;
	}

	public void setMongoDao(MongoDBDaoImpl mongoDao) {
		this.mongoDao = mongoDao;
	}

	public IBaseDao getDao() {
		return dao;
	}

	public void setDao(IBaseDao dao) {
		this.dao = dao;
	}

	@RequestMapping("/test")
	public ModelAndView test() {
		User u = new User();
		u.setUsername("zhangsan");
		u.setAge(100);
		String json = JSON.toJSONString(u);
		// dao.saveOrUpdate(u);
		System.out.println("chenggong!");
		System.out.println(json);

		String j = "{\"age\":100,\"username\":\"zhangsan\"}";
		User ur = JSON.parseObject(j, User.class);

		System.out.println(ur.getUsername() + "==" + ur.getAge());

		return new ModelAndView("index", "s", "maven success!");
	}

	@SuppressWarnings("unchecked")
	@RequestMapping("/mongo")
	public ModelAndView mongo() {
		// mongoDao.saveObject(new Tree("3", "Apple Tree", 10));
		Tree tree = mongoDao.findById("1", Tree.class);
		System.out.println(tree.toString());
		return new ModelAndView("index", "s", "mongodb success!");
	}

	@RequestMapping("/find")
	public ModelAndView find() {
		List<AppInfo> list = mongoDao
				.getAllObjects(AppInfo.class, "data_table");
		List<AppInfo> items = new ArrayList<AppInfo>();
		for (int i = 0; i < 30 && !list.isEmpty(); i++) {
			items.add(list.get(i));
			System.out.println(list.get(i).toString());
		}
		return new ModelAndView("appinfo", "list", items);
	}

	//测试转json
	@RequestMapping("/json")
	public ModelAndView json() {
		List<AppInfo> list = mongoDao
				.getAllObjects(AppInfo.class, "data_table");
		List<AppInfo> items = new ArrayList<AppInfo>();
		for (int i = 0; i < 5 && !list.isEmpty(); i++) {
			items.add(list.get(i));
		}
		String json = JsonParser.parseResult(items);

		
		System.out.println(json);

		return new ModelAndView("index", "jsonlist", json);
	}
	
	@RequestMapping("/jsonlist")
	public ModelAndView jsonlist() {
		return new ModelAndView("index", "s", "josnlist success!");
	}
	
	// public static final Object parse(String text); //
	// 把JSON文本parse为JSONObject或者JSONArray
	// public static final JSONObject parseObject(String text)； //
	// 把JSON文本parse成JSONObject
	// public static final T parseObject(String text, Class clazz); //
	// 把JSON文本parse为JavaBean
	// public static final JSONArray parseArray(String text); //
	// 把JSON文本parse成JSONArray
	// public static final List parseArray(String text, Class clazz);
	// //把JSON文本parse成JavaBean集合
	// public static final String toJSONString(Object object); //
	// 将JavaBean序列化为JSON文本
	// public static final String toJSONString(Object object, boolean
	// prettyFormat); // 将JavaBean序列化为带格式的JSON文本
	// public static final Object toJSON(Object javaObject);
	// 将JavaBean转换为JSONObject或者JSONArray。
}
